// pages/addbill/addbill.js

const util = require('../../utils/date.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: "2018-07-01",
    empty: ""
  },

  /**
   * 日期修改
   */
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      date: new Date().Format('yyyy-MM-dd')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      date: new Date().Format('yyyy-MM-dd')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  formSubmit: function(e) {
    var that = this

    if (e.detail.value.bill_name === '' || e.detail.value.bill_value === '' || e.detail.value.bill_amount === '') {
      wx.showToast({
        title: '名称金额数量不能为空',
        icon: 'none',
        duration: 1000
      })
      return false
    }

    if (isNaN(parseFloat(e.detail.value.bill_value))) {
      wx.showToast({
        title: '金额错误',
        icon: 'none',
        duration: 1000
      })
      return false
    }

    e.detail.value.bill_value = parseInt(parseFloat(e.detail.value.bill_value).toFixed(2) * 100)
    e.detail.value.bill_amount = parseInt(e.detail.value.bill_amount)

    if (e.detail.value.bill_value > 1000 * 100) {
      wx.showToast({
        title: '怎么会这么富有?',
        icon: 'none',
        duration: 1000
      })
      return false
    }

    var app = getApp()
    console.log({
      bill_name: e.detail.value.bill_name,
      bill_remark: e.detail.value.bill_remark === '' ? '无' : e.detail.value.bill_remark,
      bill_value: e.detail.value.bill_value,
      bill_date: that.data.date,
      bill_amount: e.detail.value.bill_amount
    })
    wx.showModal({
      title: '账单确认',
      content: '确定提交此账单?',
      success: function(res) {
        if (res.confirm) {
          // console.log('bill sent', e.detail.value)
          wx.request({
            url: app.globalData.apiUrl + '/add_bill',
            method: 'POST',
            data: {
              bill_name: e.detail.value.bill_name,
              bill_remark: e.detail.value.bill_remark === '' ? '无' : e.detail.value.bill_remark,
              bill_value: e.detail.value.bill_value,
              bill_date: that.data.date,
              bill_amount: e.detail.value.bill_amount
            },
            header: {
              'content-type': 'application/json'
            },
            success: function(res) {
              if (res.statusCode === 200) {
                console.log(res.data)
                that.setData({
                  empty: ""
                })
                wx.showToast({
                  title: '提交成功',
                  icon: 'success',
                  duration: 1000
                })
              } else {
                wx.showToast({
                  title: '提交失败',
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  },
})